package com.mrx.example.controller;

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mrx.example.dao.EmpDao;
import com.mrx.example.model.Emp;
import com.mrx.example.model.PostBody;
import com.mrx.example.model.Result;
import com.mrx.example.servlet.BaseServlet;

@WebServlet(name = "编辑 API", value = "/edit")
public class EditController extends BaseServlet {

    private final EmpDao empDao = new EmpDao();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PostBody res = getPostBody(req);
        String type = req.getParameter("type");
        // 如果 postBody 或 type 为空 或 postBody 不含 emp
        if (res.hasNull() || hasBlankOrNull(type) || res.dataNotContains("emp")) {
            send404ModelResponse(resp, Result.E404);
        } else {
            Emp emp = res.getJavaObjectFromJSON("emp", Emp.class);
            if (type.equals("emp")) {
                sendModelResponse(resp, empDao.updateEmp(emp));
            }
        }
    }

}
